package soms_handler

import (
	"encoding/json"
	"gitlab.yoyiit.com/youyi/go-core/handler"
	"gitlab.yoyiit.com/youyi/go-core/util"
	"github.com/gin-gonic/gin"
	"github.com/pkg/errors"
	"strconv"
	"time"
)

type CounterpartyContactHandler interface {
	ListCounterpartyContact() gin.HandlerFunc
	GetCounterpartyContact() gin.HandlerFunc
	AddCounterpartyContact() gin.HandlerFunc
	EditCounterpartyContact() gin.HandlerFunc
	DeleteCounterpartyContact() gin.HandlerFunc
	BatchDeleteCounterpartyContact() gin.HandlerFunc
}

type counterpartyContactHandler struct {
	somsClient soms.Client
}

func (h *counterpartyContactHandler) ListCounterpartyContact() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var req stru.ListCounterpartyContactDataRequest
		if err := ctx.ShouldBindQuery(&req); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		res, err := h.somsClient.ListCounterpartyContact(util.GetRPCContext(ctx), &api.ListCounterpartyContactRequest{
			PageNum:  req.PageNum,
			PageSize: req.PageSize,
			Data: &api.CounterpartyContactData{
				CounterpartyId: req.CounterpartyId,
	ContactName: req.ContactName,
	ContactPhone: req.ContactPhone,
	ContactAddress: req.ContactAddress,
	ProvinceName: req.ProvinceName,
	ProvinceCode: req.ProvinceCode,
	CityName: req.CityName,
	CityCode: req.CityCode,
	AreaName: req.AreaName,
	AreaCode: req.AreaCode,
	
			},
		})
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		if res == nil || res.Data == nil {
			handler.HandleSuccessListDataResponse(ctx, nil, 0)
			return
		}
		handler.HandleSuccessListDataResponse(ctx, res.Data, res.Count)
	}
}

func (h *counterpartyContactHandler) GetCounterpartyContact() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		res, err := h.somsClient.GetCounterpartyContact(util.GetRPCContext(ctx), &api.CounterpartyContactData{
			Id: id,
		})
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessDataResponse(ctx, res)
	}
}

func (h *counterpartyContactHandler) AddCounterpartyContact() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var data stru.CounterpartyContactData
		if err := ctx.ShouldBindJSON(&data); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		addData := &api.CounterpartyContactData{
            Id:         data.Id,
            CounterpartyId: data.CounterpartyId,
	ContactName: data.ContactName,
	ContactPhone: data.ContactPhone,
	ContactAddress: data.ContactAddress,
	ProvinceName: data.ProvinceName,
	ProvinceCode: data.ProvinceCode,
	CityName: data.CityName,
	CityCode: data.CityCode,
	AreaName: data.AreaName,
	AreaCode: data.AreaCode,
	
        }
		err := h.somsClient.AddCounterpartyContact(util.GetRPCContext(ctx), addData)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *counterpartyContactHandler) EditCounterpartyContact() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		var data stru.CounterpartyContactData
		if err = ctx.ShouldBindJSON(&data); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}

		editData := &api.CounterpartyContactData{
            Id:         id,
            CounterpartyId: data.CounterpartyId,
	ContactName: data.ContactName,
	ContactPhone: data.ContactPhone,
	ContactAddress: data.ContactAddress,
	ProvinceName: data.ProvinceName,
	ProvinceCode: data.ProvinceCode,
	CityName: data.CityName,
	CityCode: data.CityCode,
	AreaName: data.AreaName,
	AreaCode: data.AreaCode,
	
        }
		if err = h.somsClient.EditCounterpartyContact(util.GetRPCContext(ctx), editData); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *counterpartyContactHandler) DeleteCounterpartyContact() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		if err = h.somsClient.DeleteCounterpartyContact(util.GetRPCContext(ctx), id); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *counterpartyContactHandler) BatchDeleteCounterpartyContact() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var req stru.BatchDeleteCounterpartyContactRequest
		if err := ctx.ShouldBindJSON(&req); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}

		if req.Ids == nil || len(req.Ids) == 0 {
			handler.HandleErrorResponse(ctx, handler.HandleNewError("参数错误"))
			return
		}

		if err := h.somsClient.BatchDeleteCounterpartyContact(util.GetRPCContext(ctx), &api.BatchDeleteCounterpartyContactRequest{
			Ids: req.Ids,
		}); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

